import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入登录组件
// import Login from '../views/Login.vue'
// import Home from '../views/Home.vue'
// import Welcome from '../views/Welcome.vue'
const Login = () => import(/* webpackChunkName: "login-home-welcome" */ '../views/Login.vue')
const Home = () => import(/* webpackChunkName: "login-home-welcome" */ '../views/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login-home-welcome" */ '../views/Welcome.vue')

// @ 是 vue 给提供的一个路径的简写，代表 src 目录
// import Users from '@/views/Users/Users.vue'
const Users = () => import(/* webpackChunkName: "users" */ '@/views/Users/Users.vue')

// import Rights from '@/views/Power/Rights.vue'
// import Roles from '@/views/Power/Roles.vue'
const Rights = () => import(/* webpackChunkName: "right_roles" */ '@/views/Power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "right_roles" */ '@/views/Power/Roles.vue')

// import Category from '@/views/goods/Category.vue'
// import Params from '@/views/goods/Params.vue'
// import List from '@/views/goods/List.vue'
// import AddGoods from '@/views/goods/AddGoods.vue'
const Category = () => import(/* webpackChunkName: "goods" */ '@/views/goods/Category.vue')
const Params = () => import(/* webpackChunkName: "goods" */ '@/views/goods/Params.vue')
const List = () => import(/* webpackChunkName: "goods" */ '@/views/goods/List.vue')
const AddGoods = () => import(/* webpackChunkName: "goods" */ '@/views/goods/AddGoods.vue')

// import OrderList from '@/views/order/OrderList.vue'
const OrderList = () => import(/* webpackChunkName: "orderlist" */ '@/views/order/OrderList.vue')

// import Reports from '@/views/Reports/Reports.vue'
const Reports = () => import(/* webpackChunkName: "reports" */ '@/views/Reports/Reports.vue')

Vue.use(VueRouter)

const routes = [
  // 路由重定向
  { path: '/', redirect: '/login' },
  // 配置登录路由规则
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Category },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/addgoods', component: AddGoods },
      { path: '/orders', component: OrderList },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to -- 要跳转的地址
  // from -- 从那个网页跳转过来
  // next -- 方形，执行下一步操作

  // 如果用户访问的是登录页面，那么直接放行，让用户访问登录页面
  if (to.path === '/login') return next()

  // 获取 token ，如果 token 的值是 null ，说明用户没有登录，否则用户处在登录状态
  const myToken = window.sessionStorage.getItem('token')

  if (!myToken) return next('/login')

  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// ————————————————
// 版权声明：本文为CSDN博主「周不会前端」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
// 原文链接：https://blog.csdn.net/ZHOU_CXY/java/article/details/106565480

export default router
